﻿#include "picture.h"
#include "dhlz_type.h"
#include "mycrc.h"
#include <QApplication>
#include <QWidget>
#include <QDir>
#include <QFile>
#include <QDebug>

picture::picture(void)
{
    bWidth = 0;
    bHeight = 0;
    bBitCount = 0;
}

picture::~picture()
{
}

bool picture::push(void const *p_data, unsigned long size)
{
    bWidth = 0;
    bHeight = 0;
    bBitCount = 0;
    const char *p_str = (const char *)p_data;

    if ((p_str[0] == 'B') && (p_str[1] == 'M'))
    {
        unsigned long biSize = str_to_hex32(&p_str[14]);

        if ((biSize + 14) <= size)
        {
            bWidth = str_to_hex32(&p_str[14 + 4]);
            bHeight = str_to_hex32(&p_str[14 + 8]);
            bBitCount = str_to_hex16(&p_str[14 + 14]);
        }

        qDebug() << "picture::push" << biSize << bWidth << bHeight << bBitCount;
        return true;
    }

    return false;
}

long picture::Width(void)
{
    return bWidth;
}
long picture::Height(void)
{
    return bHeight;
}
long picture::BitCount(void)
{
    return bBitCount;
}

unsigned short picture::str_to_hex16(const char *str)
{
    return ((str[0] & 0x0FF) | ((str[1] << 8) & 0x0FF00));
}

unsigned long picture::str_to_hex32(const char *str)
{
    return ((str[0] & 0x0FF) | ((str[1] << 8) & 0x0FF00) | ((str[2] << 16) & 0x0FF0000) | ((str[3] << 24) & 0xFF000000));
}
